package com.ygs.secs.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ygs.secs.model.Record;
import com.ygs.secs.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 应聘记录控制器
 */
@Controller
@RequestMapping("/ygssecs")
public class RecordController {
    @Autowired
    RecordService recordService;

    /**
     * 获取应聘记录表
     *
     * @param jobid 岗位ID
     * @return 通用json返回格式
     */
    @RequestMapping("/getRecordListbyjobid")
    @ResponseBody
    public Map<String, Object> getRecordListbyjobid(String jobid, @RequestParam(defaultValue = "1") int pageNum, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        //分页查询
        Page startPage = PageHelper.startPage(pageNum, pageSize);
        try {
            List<Record> recordList = recordService.getRecordlistByjobid(jobid);
            if (recordList.size() > 0) {
                map.put("code", 200);
                map.put("msg", "应聘数据查询成功");
                map.put("count", startPage.getTotal());
                map.put("data", recordList);
                map.put("pages", startPage.getPages());
            } else {
                map.put("code", 500);
                map.put("msg", "还没有应聘者来应聘");
                map.put("count", startPage.getTotal());
                map.put("data", recordList);
                map.put("pages", startPage.getPages());
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "服务器错误");
            map.put("data", null);
        }
        return map;
    }

    /**
     * 通过记录ID删除招聘记录
     *
     * @param recordid 招聘记录ID
     * @return 通用json返回格式
     */
    @RequestMapping("/deleteRecordbyrecordid")
    @ResponseBody
    public Map<String, Object> deleteRecordbyrecordid(int recordid) {
        Map<String, Object> map = new HashMap<>();
        int delcnt = recordService.deleteByPrimaryKey(recordid);
        if (delcnt > 0) {
            map.put("code", 200);
            map.put("msg", "拒绝成功!");
        } else {
            map.put("code", 500);
            map.put("msg", "拒绝失败！");
        }
        return map;
    }


    /**
     * 通过id查询应聘记录
     *
     * @param recordid
     * @return
     */
    @RequestMapping("/findRecordsById")
    @ResponseBody
    public Map<String, Object> findRecordsById(int recordid) {
        Map<String, Object> map = new HashMap<>();
        try {
            Record record = recordService.findrecordbyid(recordid);
            if (record != null) {
                map.put("code", 200);
                map.put("msg", "查询成功");
                map.put("data", record);
            } else {
                map.put("code", 201);
                map.put("msg", "查询失败");
                map.put("data", null);
            }
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
            map.put("data", null);
        }
        return map;
    }

}
